package com.top.dao;

import com.top.entiy.FoodCategory;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author:
 * @DateTime: 2023/12/8 21:52
 * @Description: TODO
 */
public class CategoryFoodDao extends AbstractDAO implements IDAO<Integer, FoodCategory> {




    //INSERT INTO `ordering_food`.`food_category` (`id`, `parent_id`, `category_name`, `sort`, `create_time`, `upate_time`) VALUES (1, 0, '11', 1, '2023-12-08 23:05:41', '2023-12-08 23:05:44');

    @Override
    public boolean doCreate(FoodCategory vo){
        String sql = "INSERT INTO `ordering_food`.`food_category` (`id`, `parent_id`, `category_name`, `sort`, `create_time`, `upate_time`) " +
                "VALUES (NULL, 0, ?, ?,now(), now())";
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1,vo.getCategoryName());
            statement.setInt(2,vo.getSort());
            return super.statement.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("创建失败"+e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean doUpdate(FoodCategory vo){
        try {
            String sql = "UPDATE food_category SET " +
                    "category_name= ? , sort = ? , upate_time = NOW() " +
                    "WHERE id = ?";
            statement =conn.prepareStatement(sql);
            statement.setString(1,vo.getCategoryName());
            statement.setInt(2,vo.getSort());
            statement.setInt(3, vo.getId());
            return statement.executeUpdate()>0;
        } catch (SQLException e) {
            System.err.println("更新失败"+e);
        }
        return false;
    }

    @Override
    public boolean doRemove(Integer id) {
        try {
            String sql = "DELETE FROM food_category WHERE id = ?";
            statement = conn.prepareStatement(sql);
            statement.setInt(1,id);
            return statement.executeUpdate()>0;
        }catch (SQLException ex) {
            System.err.println("删除失败"+ex);
        }
        return false;
    }


    @Override
    public FoodCategory findById(Integer id) {
        String sql = "SELECT * FROM food_category where id =?";
        try {
            statement = conn.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                FoodCategory vo = new FoodCategory();
                vo.setId(rs.getInt("id"));
                vo.setCategoryName(rs.getString("category_name"));
                vo.setSort(rs.getInt("sort"));
                vo.setCreateTime(rs.getDate("create_time"));
                return vo;
            }
        } catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return null;
    }
    public FoodCategory findByName(String name){
        String sql = "SELECT * FROM food_category where category_name = ?";
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1,name);
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                FoodCategory vo = new FoodCategory();
                vo.setId(rs.getInt("id"));
                vo.setCategoryName(rs.getString("category_name"));
                vo.setSort(rs.getInt("sort"));
                vo.setCreateTime(rs.getDate("create_time"));
                return vo;
            }
        }catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return null;
    }

    @Override
    public List<FoodCategory> findAll() {

        List<FoodCategory> all = new ArrayList<FoodCategory>();
        try {
            String sql = "SELECT * FROM food_category ORDER BY sort";
            statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                FoodCategory vo = new FoodCategory();
                vo.setId(rs.getInt("id"));
                vo.setCategoryName(rs.getString("category_name"));
                vo.setSort(rs.getInt("sort"));
                vo.setCreateTime(rs.getDate("create_time"));
                vo.setUpateTime(rs.getDate("upate_time"));
                all.add(vo);
            }
            return all;
        } catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return all;
    }

    @Override
    public List<FoodCategory> findAllBySplit(String column, String keyWord, Integer currentPage, Integer lineSize) throws SQLException {
        return null;
    }

    @Override
    public Integer getAllCount(String column, String keyWord) throws SQLException {
        return null;
    }

}
